#include "LLogicGroup.h"
#include "GLGlobalProtocol.h"
#include "scene/LayerGroup.h"

#define GET_GROUP_UI_RETURN_VOID(Ui) \
	CLayerGroup* Ui = (CLayerGroup*)m_CommonUi->GetCommonNode(CCommonUiManager::CGroupIndex); \
	if (nullptr == Ui) { \
		CCLOG("error|group|create|layer|null|%s", __FUNCTION__); \
		return; \
	}

CLLogicGroup::CLLogicGroup(void)
{
}


CLLogicGroup::~CLLogicGroup(void)
{
}

void CLLogicGroup::LogicInit() {
	//	AddLogicType(DCLTUserInfo);
	//	AddLogicType(DCLTProperty);
	//	AddLogicType(DCLTSkill);
	//	AddLogicType(DCLTPropertyPoint);
}

bool CLLogicGroup::CatchLogic(const ProtocolBase *head, char * data, uint32_t len, void * connect) {
	LSocket* tConnect = (LSocket*)(connect);
	switch (head->m_Logic) {
	case GLSLTGroupPerson:
		if (head->m_From == GLPFromServer) {
			switch (head->m_Action) {
			case GLPAGet:
				onGetList(head, data, len);
				break;
			case GLPACreate:
				onCreate(head, data, len);
				break;
			case GLPAApply:
				onApply(head, data, len);
				break;
			case GLPAOtherApply:
				onOtherApply(head, data, len);
				break;
			case GLPAJoin:
				onJoin(head, data, len);
				break;
			case GLPAOtherJoin:
				onOtherJoin(head, data, len);
				break;
			case GLPALeave:
				onLeave(head, data, len);
				break;
			case GLPAOtherLeave:
				onOtherLeave(head, data, len);
				break;
			case GLPAAdd:
				onAddGroup(head, data, len);
				break;
			case GLPADel:
				onDelGroup(head, data, len);
				break;
			case GLPADownload:
				onDownloadGroup(head, data, len);
				break;
			default:
				return false;
				break;
			}
		}
		break;
	default:
		return false;
		break;
	}
	return true;
}

void CLLogicGroup::Create() {
	CreateGroupReq	tCreate;
	tCreate.m_UserId = m_Oneself->GetBaseInfo().m_Uid;

	m_LogicConnect->SendLenAndData((const char*)&tCreate, sizeof(tCreate));
}

void CLLogicGroup::Apply(const uint64_t& otherUid) {
	ApplyGroupReq	tApply;
	tApply.m_UserId = m_Oneself->GetBaseInfo().m_Uid;
	tApply.m_Extend = otherUid;

	m_LogicConnect->SendLenAndData((const char*)&tApply, sizeof(tApply));
}

void CLLogicGroup::Agree(const uint64_t& otherUid, ssize_t index) {
	AgreeGroupReq tAgree;
	tAgree.m_UserId = m_Oneself->GetBaseInfo().m_Uid;
	tAgree.m_Extend = otherUid;

	m_LogicConnect->SendLenAndData((const char*)&tAgree, sizeof(tAgree));

	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup)
		return;
	CGLGroup::VecLook_t& tAll = tGroup->AllApply();
	if (tAll.size() > index)
		tAll.erase(tAll.begin() + index);
}

void CLLogicGroup::Leave() {
	LeaveGroupReq	tLeave;
	tLeave.m_UserId = m_Oneself->GetBaseInfo().m_Uid;

	m_LogicConnect->SendLenAndData((const char*)&tLeave, sizeof(tLeave));

	m_LogicData->RemGroup();

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushMyGroup();
}

void CLLogicGroup::GetList() {
	GetGroupListReq	tGetList;
	tGetList.m_UserId = m_Oneself->GetBaseInfo().m_Uid;

	m_LogicConnect->SendLenAndData((const char*)&tGetList, sizeof(tGetList));
}

void CLLogicGroup::onGetList(const ProtocolBase *head, char * data, uint32_t len) {
	GetGroupListResp* tGetList = (GetGroupListResp*)(data);

	for (uint8_t i = 0; i != tGetList->m_Count; ++i) {
		m_LogicData->AddTempGroup(tGetList->m_Groups.m_Leader[i]);
	}

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushListGroup();
}

void CLLogicGroup::onCreate(const ProtocolBase *head, char * data, uint32_t len) {
	CreateGroupReq* tCreate = (CreateGroupReq*)(data);
	if (tCreate->m_Extend == CommonErrnoSucceed) {
		CGLGroup* tGroup = m_LogicData->GetGroup();
		if (nullptr != tGroup) {
			CCLOG("error|group|create|exist");
			return;
		}
		tGroup = m_LogicData->CreateGroup();
		AnimalLookInfo tInfo;
		m_Oneself->GetLookInfo(tInfo);
		tGroup->Add(tInfo);

		GET_GROUP_UI_RETURN_VOID(tLayerGroup);
		tLayerGroup->FlushMyGroup();
	} else {
		CCLOG("error|group|create|errno|%d", tCreate->m_Extend);
	}
}

void CLLogicGroup::onApply(const ProtocolBase *head, char * data, uint32_t len) {
	ApplyGroupReq* tApply = (ApplyGroupReq*)(data);
	if (tApply->m_Extend != CommonErrnoSucceed) {
		CCLOG("error|group|apply|errno|%d", tApply->m_Extend);
	}
}

void CLLogicGroup::onOtherApply(const ProtocolBase *head, char * data, uint32_t len) {
	ApplyGroupToLeader* tApply = (ApplyGroupToLeader*)(data);
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup) {
		CCLOG("error|group|null|%s", __FUNCTION__);
		return;
	}

	tGroup->AddApply(tApply->m_AnimalInfo);
	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushTempUser();
}

void CLLogicGroup::onJoin(const ProtocolBase *head, char * data, uint32_t len) {
	JoinGroupReq* tJoin = (JoinGroupReq*)(data);
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr != tGroup) {
		CCLOG("error|group|exist");
		return;
	}

	tGroup = m_LogicData->CreateGroup();
	for (int8_t i = 0; i != tJoin->m_AnimalNum; ++i) {
		tGroup->Add(tJoin->m_AnimalInfo[i]);
	}

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushMyGroup();
}

void CLLogicGroup::onOtherJoin(const ProtocolBase *head, char * data, uint32_t len) {
	OtherJoinGroupReq* tJoin = (OtherJoinGroupReq*)(data);
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup) {
		CCLOG("error|group|null|%s", __FUNCTION__);
		return;
	}

	tGroup->Add(tJoin->m_AnimalInfo);
	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushMyGroup();
}

void CLLogicGroup::onLeave(const ProtocolBase *head, char * data, uint32_t len) {
	LeaveGroupReq* tLeave = (LeaveGroupReq*)(data);
	if (tLeave->m_Extend != CommonErrnoSucceed) {
		CCLOG("error|group|leave|errno|%d", tLeave->m_Extend);
		return;
	}
}

void CLLogicGroup::onOtherLeave(const ProtocolBase *head, char * data, uint32_t len) {
	OtherLeaveGroupReq* tLeave = (OtherLeaveGroupReq*)(data);
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup) {
		CCLOG("error|group|null|%s", __FUNCTION__);
		return;
	}

	if (tGroup->Rem(tLeave->m_UserId))
		m_LogicData->RemGroup();
	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushMyGroup();
}

void CLLogicGroup::onAddGroup(const ProtocolBase *head, char * data, uint32_t len) {
	AddGroupResp* tAdd = (AddGroupResp*)(data);

	m_LogicData->AddTempGroup(tAdd->m_Group);

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushListGroup();
}

void CLLogicGroup::onDelGroup(const ProtocolBase *head, char * data, uint32_t len) {
	DelGroupResp* tDel = (DelGroupResp*)(data);

	m_LogicData->DelTempGroup(tDel->m_Group);

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushListGroup();
}

void CLLogicGroup::onDownloadGroup(const ProtocolBase *head, char * data, uint32_t len) {
	DownloadGroupResp* tLoad = (DownloadGroupResp*)(data);
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr != tGroup) {
		CCLOG("error|group|exist|%s", __FUNCTION__);
		return;
	}

	tGroup = m_LogicData->CreateGroup();
	for (int32_t i = 0; i != GroupCount; ++i) {
		const AnimalLookInfo& tInfo = tLoad->m_AnimalInfo[i];
		if (tInfo.m_Type != EntityTypeNone) {
			tGroup->Add(tInfo);
		}
	}

	GET_GROUP_UI_RETURN_VOID(tLayerGroup);
	tLayerGroup->FlushMyGroup();
}
